from django.template import RequestContext
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from django.core import serializers
from django.http import HttpResponse, HttpResponseNotFound,HttpResponseRedirect
from django.http import Http404
from django.conf import settings
from sync.models import SFObject,SFField
from sforce.enterprise import SforceEnterpriseClient
from wizard.forms import *
from portal.models import View,ViewField,get_view,create_view
def import_objects(request):
    gl=SFObject.import_objects()
    return render_to_response('wizard/import_objects.html', 
                              {'gl': gl},
                              context_instance = RequestContext(request))
    #return HttpResponseRedirect('/wizard/all_objects/')
def all_objects(request):
    local=request.GET.get("local",False)
    gl=SFObject.objects.all()
    if local:
        gl=gl.exclude(local_table__isnull=True)
    return render_to_response('wizard/all_objects.html', 
                              {'gl': gl},
                              context_instance = RequestContext(request))
def import_sf_object(request,sf_object):
    sf_obj=SFObject.objects.get(name=sf_object)
    sobj=sf_obj.set_elements()
    return render_to_response('wizard/import_sf_object.html', 
                              {'sobj': sobj,
                               "sf_obj":sf_obj},
                              context_instance = RequestContext(request))

def sf_object(request,sf_object):
    sf_obj=SFObject.objects.get(name=sf_object)
    fields=SFField.objects.filter(obj=sf_obj)
    views=View.objects.filter(obj=sf_obj)
    if fields.count() <=0:
        sobj=sf_obj.set_elements()
    #sobj=sf_obj.set_elements()
   
    return render_to_response('wizard/sf_object.html', 
                              {'sf_obj': sf_obj,
                               'views':views},
                              context_instance = RequestContext(request))


def local_table(request,sf_object):
    sf_obj=SFObject.objects.get(name=sf_object)
    sf_fields=SFField.objects.filter(obj=sf_obj)
    
    return render_to_response('wizard/local_table.html', 
                              {'sf_obj': sf_obj,
                               'sf_fields':sf_fields},
                              context_instance = RequestContext(request))

def add_fields(request,sf_object):
    sf_obj=SFObject.objects.get(name=sf_object)
    fields=SFField.get_add_fields(sf_obj)
    if request.method=="POST":
        #is the table created?
        if not sf_obj.local_table:
            for f in fields:
                on=request.POST.get("fid_%d"%f.pk,False)
                if on:
                    f.set_local_fld()
                    f.save()
            sf_obj.create_table()
        else:
            for f in fields:
                on=request.POST.get("fid_%d"%f.pk,False)
                if on:
                    f.add_t()
                else:
                    f.remove_t()
            
        return HttpResponseRedirect("/wizard/all_objects/")
    else:
        return render_to_response('wizard/add_fields.html', 
                              {'sf_obj': sf_obj,
                               'fields':fields},
                              context_instance = RequestContext(request))
def clear(request,sf_object):
    sf_obj=SFObject.objects.get(name=sf_object)
    sf_obj.clear()
    return HttpResponseRedirect("/wizard/all_objects/")

def edit_view(request):
    if request.method=="POST":
        view_id=request.POST.get("view_id",None)
        sf_object_id=request.POST.get("sf_object_id",None)
        sf_object=SFObject.objects.get(pk=int(sf_object_id))
        view_type=request.POST.get("vtype",'single')
        print view_type
        view_name=request.POST.get("name",'')
        if view_id:
            view=get_view(view_id)
        else:
            view=create_view(view_type)
            view.obj=sf_object

        #TODO: save form data to view
        view.name=view_name
        view.save()
        return HttpResponseRedirect("/wizard/edit_view_fields/%d/"%view.pk)
    else:
        view_id=request.GET.get("view_id",'')
        view_type=request.GET.get("view_type",'single')
        sf_object_id=request.GET.get("sf_object_id",'')
        view=None
        if view_id:
            view=get_view(view_id)
            sf_obj=view.obj
        else:
            sf_obj=SFObject.objects.get(pk=int(sf_object_id))
        view_name=""
        if view_type == 'single':
            view_name=sf_obj.label.lower()
        elif view_type == 'multi':
            view_name=sf_obj.labelPlural.lower()
        elif view_type == 'form':
            view_name="add_%s "%sf_obj.label.lower()
        elif view_type == 'custom':
            view_name="custom_%s "%sf_obj.label.lower()
        form=ViewForm(initial={"sf_object_id":sf_object_id,
                               "view_id":view_id,
                               'name':view_name,
                               'label':sf_obj.label,
                               'vtype':view_type
                               })
        return render_to_response('wizard/edit_view/view.html', 
                              {'form': form,
                               'sf_obj':sf_obj,
                               'view':view},
                              context_instance = RequestContext(request))
def edit_view_fields(request,view_id):
    view=View.objects.get(pk=int(view_id))
    fields=view.get_edit_fields()
    obj_fields=view.get_object_fields()
    if request.method=="POST":
        #edit fields
        vfld=ViewField.objects.filter(view=view)
        for f in vfld:
            delete=request.POST.get("%d_delete"%f.pk,"")
            if delete:
                f.delete()
            else:
                f.label=request.POST.get("%d_label"%f.pk,"")
                f.save()
        #add fields
        for i in range(15):
            field_id=request.POST.get("new_field_%d"%i,"")
            if field_id:
                sf_fld=SFField.objects.get(pk=field_id)
                label=request.POST.get("new_label_%d"%i,'')
                if not label:
                    label=sf_fld.label
                #order=int(request.POST.get("new_order_%d"%i,"0"))
                vfld=ViewField(view=view,field=sf_fld,label=label)
                vfld.save()
        
        #view.save_fields(request)
        return HttpResponseRedirect("/wizard/preview/%d/"%view.pk)
    else:
        return render_to_response('wizard/edit_view_fields.html', 
                              {'view': view,
                               'fields':fields,
                               'obj_fields':obj_fields,
                               'ef':range(16)},
                              context_instance = RequestContext(request))
def preview(request,view_id):
    view=get_view(view_id)
    return view.response(request,True)

def sync_local2sf(request,object_id):
    sf_obj=SFObject.objects.get(pk=object_id)
    sf_obj.synch_local2sf()
    return HttpResponseRedirect('/wizard/all_objects/?local=1')
def sync_sf2local(request,object_id):
    sf_obj=SFObject.objects.get(pk=object_id)
    sf_obj.synch_sf2local()
    return HttpResponseRedirect('/wizard/all_objects/?local=1')
